<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24
 * Time: 9:43
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use other\Tree;
use think\Db;
use think\Loader;
use think\Request;

class Category extends AdminBase
{
    public function index()
    {
        $chk = check_juri(request()->controller(), 'index', $this->_juri);
        if ($chk['status'])
        {
            $list = Db::name('category')
                ->alias('c')
                ->field('c.id, c.title, c.other_name, c.thumb, c.parentid, c.type, c.sort, c.is_show, c.update_time, cc.title as parent_name')
                ->join('__CATEGORY__ cc', 'c.parentid = cc.id', 'LEFT')
                ->order('c.sort asc, c.create_time desc')
                ->select();
            $tree = new Tree($list);
            $list = $tree->get_tree(0);
            return view('index', [
                'list'=>$list,
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function add()
    {
        $chk = check_juri(request()->controller(), 'add', $this->_juri);
        if ($chk['status'])
        {
            $parentid = intval(input('parentid'));
            $list = Db::name('category')
                ->field('id, title, parentid')
                ->order('sort desc, create_time desc')
                ->select();
            $tree = new Tree($list);
            $list = $tree->get_tree(0);
            $sort = Loader::model('Category')->max('sort');
            return view('add', [
                'parentid'=>$parentid,
                'list'    =>$list,
                'sort'    =>$sort+1
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }
    }

    public function doAdd()
    {
        if (request()->isPost())
        {
            $data   = Request::instance()->post();
            $result = $this->validate($data, 'Category');
            if ($result !== true)
            {
                return ['status'=>false, 'msg'=>$result];
            }
            $info   = img_upload_one('thumb');
            if ($info)
            {
                if ($info['status'])
                {
                    $data['thumb'] = $info['msg'];
                } else {
                    return ['status'=>false, 'msg'=>$info['msg']];
                }
            }
            $system = Loader::model('Category');
            $system->cateAdd($data);
            return ['status'=>true, 'msg'=>$data];
        }
    }

    public function edit()
    {
        $chk = check_juri(request()->controller(), 'edit', $this->_juri);
        if ($chk['status'])
        {
            $id   = intval(input('param.id'));
            $info = Db::name('category')
                ->field('id, parentid, title, other_name, thumb, sort, attr, is_show, description, seo_title, seo_desc, seo_keywords, list_tpl, article_tpl, url, type')
                ->where('id', $id)
                ->find();
            $list = Db::name('category')
                ->field('id, title, parentid')
                ->order('sort desc, create_time desc')
                ->select();
            $tree = new Tree($list);
            $list = $tree->get_tree(0);
            $sort = Loader::model('Category')->max('sort');
            return view('edit', [
                'info'    =>$info,
                'list'    =>$list,
                'sort'    =>$sort+1
            ]);
        } else {
            $this->error($chk['msg'], url('admin/index/index'), 3);
        }

    }

    public function doEdit()
    {
        if (request()->isPost())
        {
            $data   = Request::instance()->post();
            $cate   = Db::name('Category')->field('thumb')->where('id', $data['id'])->find();
            $result = $this->validate($data, 'Category');
            if ($result !== true)
            {
                return ['status'=>false, 'msg'=>$result];
            }
            $info   = img_upload_one('thumb');
            if ($info)
            {
                if ($info['status'])
                {
                    $path          = './public/'.$cate['thumb'];
                    if (file_exists($path))
                    {
                        @unlink($path);
                    }
                    $data['thumb'] = $info['msg'];
                } else {
                    return ['status'=>false, 'msg'=>$info['msg']];
                }
            } else {
                if (isset($data['thumb_del']))
                {
                    $data['thumb'] = '';
                } else {
                    $data['thumb'] = $cate['thumb'];
                }
            }
            $system = Loader::model('Category');
            $system->cateEdit($data);
            return ['status'=>true, 'msg'=>$data];
        }
    }

    public function del()
    {
        if (request()->isPost())
        {
            $chk = check_juri(request()->controller(), 'del', $this->_juri);
            if ($chk['status'])
            {
                $id   = intval(input('param.id'));
                Loader::model('Category')->del($id);
                return ['status'=>true, 'msg'=>'删除成功'];
            } else {
                return ['status'=>false, 'msg'=>$chk['msg']];
            }
        }
    }
}